﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Components;
using System.Data;
using System.Windows.Forms;
using Core.Base;

namespace Core.Component
{
    public enum CalcFrom
    {
        Main = 0,
        Sub = 1
    }
	public class SGVSearchScreen:SGVScreen
    {
        private Components.SGVPanel groupBox1;
        private Components.SGVLabel label1;
        private SGVGridView sgvGridView1;
        private Components.SGVPanel groupBox2;

        public int SelectedID = 0; 

        public String strSearchTable;
        private Button button1;
        private TextBox textBox1;
        private System.ComponentModel.IContainer components;
        public List<String> lstFieldToDisplays = new List<string>();

        public SGVSearchScreen()
            : base()
        {
            InitializeComponent();
            this.MaximizeBox = false;

            sgvGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.StartPosition = FormStartPosition.CenterParent;
            this.sgvGridView1.AllowUserToAddRows = false;
            this.sgvGridView1.CellDoubleClick += new DataGridViewCellEventHandler(sgvGridView1_CellDoubleClick);
        }

        void sgvGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            if (sgvGridView1.CurrentCell == null) return;

            DataRowView row = (DataRowView)sgvGridView1.CurrentCell.OwningRow.DataBoundItem;

            if (row["ID"] == null) return;

            SelectedID = Convert.ToInt32(row["ID"]);
            this.Close();
        }

        void sgvGridView1_KeyUp(object sender, KeyEventArgs e)
        {
            if (sgvGridView1.RowCount == 0) return;
            if (sgvGridView1.SelectedRows.Count == 0) return;
            if (sgvGridView1.SelectedRows[0].Cells["ID"] == null) return;

            if (e.KeyCode == Keys.Enter)
            {
                int iObjectID = Convert.ToInt32(sgvGridView1.SelectedRows[0].Cells["ID"]);
                module.InvalidateMainObject(iObjectID);
            }
        }

        public override void RegisterModule(Base.BaseModule _module)
        {
            base.RegisterModule(_module);
        }

        public void Init(String strTable, DataTable dt,  String strColumnTags)
        {
            strSearchTable = strTable;
            InitColumns(strColumnTags);
            sgvGridView1.ChangeApperance();
            sgvGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            sgvGridView1.ReadOnly = true;

            if (dt != null)
            {
                sgvGridView1.AutoGenerateColumns = false;
                sgvGridView1.DataSource = dt;
                sgvGridView1.Refresh();
            }
        }

        public void InitColumns(String strColumnTags)
        {
            String[] strColumns = strColumnTags.Split('|');

            sgvGridView1.Columns.Clear();
            foreach (String strColumnName in strColumns)
            {
                DataGridViewColumn col = AppContext.CreateGridViewColumnFromColumnName(strColumnName);
                if (col != null)
                {
                    sgvGridView1.Columns.Add(col);
                }

            }
        }

        public void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
            this.groupBox1 = new Components.SGVPanel();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new Components.SGVLabel();
            this.groupBox2 = new Components.SGVPanel();
            this.sgvGridView1 = new Core.Component.SGVGridView();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.sgvGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.groupBox1.CausesValidation = false;
            this.groupBox1.Controls.Add(this.button1);
            this.groupBox1.Controls.Add(this.textBox1);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.SGVTag = null;
            this.groupBox1.Size = new System.Drawing.Size(666, 67);
            this.groupBox1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(553, 26);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 2;
            this.button1.Text = "Search";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // textBox1
            // 
            this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.textBox1.Location = new System.Drawing.Point(25, 27);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(522, 20);
            this.textBox1.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(163)));
            this.label1.ForeColor = System.Drawing.Color.Purple;
            this.label1.Location = new System.Drawing.Point(6, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(93, 15);
            this.label1.TabIndex = 0;
            this.label1.Text = "Search Criteria";
            // 
            // groupBox2
            // 
            this.groupBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.groupBox2.CausesValidation = false;
            this.groupBox2.Controls.Add(this.sgvGridView1);
            this.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox2.Location = new System.Drawing.Point(0, 67);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.SGVTag = null;
            this.groupBox2.Size = new System.Drawing.Size(666, 335);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.Text = "Result";
            // 
            // sgvGridView1
            // 
            this.sgvGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle1.BackColor = System.Drawing.Color.Salmon;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(163)));
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            this.sgvGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
            this.sgvGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
            dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(163)));
            dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
            dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
            this.sgvGridView1.DefaultCellStyle = dataGridViewCellStyle2;
            this.sgvGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.sgvGridView1.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
            this.sgvGridView1.EnableHeadersVisualStyles = false;
            this.sgvGridView1.Location = new System.Drawing.Point(0, 0);
            this.sgvGridView1.MultiSelect = false;
            this.sgvGridView1.Name = "sgvGridView1";
            dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(163)));
            dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            this.sgvGridView1.RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
            this.sgvGridView1.SGVTag = null;
            this.sgvGridView1.Size = new System.Drawing.Size(664, 333);
            this.sgvGridView1.TabIndex = 0;
            this.sgvGridView1.VirtualMode = true;
            // 
            // SGVSearchScreen
            // 
            this.ClientSize = new System.Drawing.Size(666, 402);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "SGVSearchScreen";
            this.Load += new System.EventHandler(this.SGVSearchScreen_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.sgvGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        private void SGVSearchScreen_Load(object sender, EventArgs e)
        {
            this.button1.Focus();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (sgvGridView1.CurrentCell != null)
            {
                if (keyData == Keys.Down && sgvGridView1.Focused == false)
                {
                    sgvGridView1.Focus();
                    sgvGridView1.CurrentCell = sgvGridView1[sgvGridView1.CurrentCell.ColumnIndex, sgvGridView1.CurrentCell.RowIndex + 1];
                }
                if (keyData == Keys.Up && sgvGridView1.Focused == true && sgvGridView1.CurrentCell.RowIndex == 0)
                {
                    textBox1.Focus();
                    textBox1.SelectAll();
                }
                if (keyData == Keys.Enter)
                {
                    DataRowView row = (DataRowView)sgvGridView1.CurrentCell.OwningRow.DataBoundItem;
                    if (row["ID"] != null)
                    {
                        SelectedID = Convert.ToInt32(row["ID"]);
                        this.Close();
                        return true;
                    }
                }
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
